package com.cloudsdo.cameratest.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.cloudsdo.cameratest.Constants;
import com.cloudsdo.cameratest.R;
import com.cloudsdo.cameratest.dao.Student;
import com.cloudsdo.cameratest.dao.StudentDao;
import com.cloudsdo.cameratest.model.LoadService;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends PermissionActivity {
    private TextView tvLog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvLog=findViewById(R.id.tvLog);
        requestPermission(new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
        });
    }

    public void start(View view) {
        startActivity(new Intent(this,FeatureActivity.class));
    }

    public void active(View view) {
        FaceEngine faceEngine = new FaceEngine();
        int activeCode = faceEngine.activeOnline(this, Constants.APP_ID, Constants.SDK_KEY);

        if (activeCode == ErrorInfo.MOK) {
            Toast.makeText(this, "激活成功", Toast.LENGTH_SHORT).show();
            faceEngine.unInit();
        } else if (activeCode == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
            Toast.makeText(this, "已经激活", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "激活失败"+activeCode, Toast.LENGTH_SHORT).show();
        }
    }

    public void register(View view) {
        startActivity(new Intent(this,RegisterActivity.class));
    }

    private ExecutorService executor = Executors.newSingleThreadExecutor();
    public void load(View view) {
        StudentDao dao= new StudentDao(getContext(), Student.class);
        List<Student> list=dao.queryAll();
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<list.size();i++){
            Student item=list.get(i);
            stringBuilder.append(item.toString());
        }
        tvLog.setText(stringBuilder.toString());

        executor.execute(new LoadService(getContext()));
    }

    public void clear(View view) {
        StudentDao dao= new StudentDao(getContext(), Student.class);
        dao.deleteAll();
    }
}
